/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * bzr_vaLoeschenVeranstaltung.java
 *
 * Created on 07.04.2011, 08:14:52
 */
package bzr_veranstaltungverwaltung;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author fhiesl
 */
public class bzr_vaLoeschenVeranstaltung extends javax.swing.JFrame {

    /** Creates new form bzr_vaLoeschenVeranstaltung */
    DefaultListModel dlm;
    public Connection con;

    public bzr_vaLoeschenVeranstaltung() throws InstantiationException, IllegalAccessException, SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://mysql.bezirksrundschau.com:3306/brdbm_1";
            con = (Connection) DriverManager.getConnection(url, "brdbm", "br2011!db");
            dlm = new DefaultListModel();
            initComponents();
            initList();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(bzr_vaLoeschenVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListVA = new javax.swing.JList();
        jButtonLoeschen = new javax.swing.JButton();
        jButtonSchliessen = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jListVA.setModel(dlm);
        jScrollPane1.setViewportView(jListVA);

        jButtonLoeschen.setText("Löschen");
        jButtonLoeschen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoeschenActionPerformed(evt);
            }
        });

        jButtonSchliessen.setText("Schließen");
        jButtonSchliessen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSchliessenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonSchliessen)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonLoeschen))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonLoeschen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addComponent(jButtonSchliessen)
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLoeschenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoeschenActionPerformed
        try {
            // TODO add your handling code here:
            Veranstaltungen va = (Veranstaltungen) jListVA.getSelectedValue();
            Statement s = (Statement) con.createStatement();
            s.executeUpdate("DELETE FROM Veranstaltungen WHERE Veranstaltungs_Id="+va.getV_id());
            initList();
        } catch (SQLException ex) {
            Logger.getLogger(bzr_vaLoeschenVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButtonLoeschenActionPerformed

    private void jButtonSchliessenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSchliessenActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButtonSchliessenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new bzr_vaLoeschenVeranstaltung().setVisible(true);
                } catch (InstantiationException ex) {
                    Logger.getLogger(bzr_vaLoeschenVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(bzr_vaLoeschenVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(bzr_vaLoeschenVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLoeschen;
    private javax.swing.JButton jButtonSchliessen;
    private javax.swing.JList jListVA;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void initList() throws SQLException {
        dlm.clear();
        Statement st = (Statement) con.createStatement();
        ResultSet rs = (ResultSet) st.executeQuery("SELECT * FROM Veranstaltungen");


        while (rs.next()) {
            Veranstaltungen va = new Veranstaltungen();
            va.setName(rs.getString("Name"));
            va.setOrt(rs.getString("Ort"));
            va.setPlz(rs.getInt("PLZ"));
            va.setStraße(rs.getString("Strasse"));
            va.setHausnr(rs.getInt("HausNr"));
            va.setStartdatum(rs.getDate("Startdatum"));
            va.setEnddatum(rs.getDate("Enddatum"));
            va.setZustaendigeGeschaeftsstelle(rs.getString("ZustaendigeGeschaeftsstelle"));
            va.setV_id(rs.getInt("Veranstaltungs_id"));
            dlm.addElement(va);
        }



    }
}


